package com.nanyang.tms.service.company.impl;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.companyrecharge.CompanyRechargeAddRequest;
import com.nanyang.tms.api.dto.request.companyrecharge.CompanyRechargeQueryRequest;
import com.nanyang.tms.api.dto.response.companyrecharge.CompanyRechargePageResponse;
import com.nanyang.tms.api.dto.response.companyrecharge.RechargeAddResponse;
import com.nanyang.tms.api.service.companyrecharge.CompanyRechargeRpcService;
import com.nanyang.tms.domain.company.CompanyReceiverDO;
import com.nanyang.tms.domain.company.CompanyRechargeDO;
import com.nanyang.tms.domain.company.CompanyRechargeQuery;
import com.nanyang.tms.domain.company.CompanyRechargeVO;
import com.nanyang.tms.manager.company.CompanyRechargeManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class CompanyRechargeRpcServiceImpl implements CompanyRechargeRpcService {

    @Autowired
    private CompanyRechargeManager companyRechargeManager;

    @Override
    public ApiResult<PageBean<CompanyRechargePageResponse>> listCompanyRecharge(CompanyRechargeQueryRequest param) {
        ApiResult<PageBean<CompanyRechargePageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyRechargeQuery query = new CompanyRechargeQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<CompanyRechargeVO> companyRechargeList = companyRechargeManager.listCompanyRecharge(query);
            PageBean<CompanyRechargePageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(companyRechargeList.getTotalItem());
            BeanUtils.copyListProperties(companyRechargeList.getData(), resultList.getData(), CompanyRechargePageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<RechargeAddResponse> save(CompanyRechargeAddRequest param) {
        ApiResult<RechargeAddResponse> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyRechargeDO companyRechargeDO = new  CompanyRechargeDO();
            BeanUtils.copyProperties(param,companyRechargeDO);
            companyRechargeDO.setGmtCreatePin(param.getPin());
            CompanyRechargeVO companyRechargeVO = companyRechargeManager.save(companyRechargeDO);
            RechargeAddResponse rechargeAddResponse = new RechargeAddResponse();
            BeanUtils.copyProperties(companyRechargeVO,rechargeAddResponse);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(rechargeAddResponse);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @Override
    public ApiResult<Integer> queryOrderState(String orderCode) {
        ApiResult<Integer> result = new ApiResult<>();
        try {
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(companyRechargeManager.queryOrderState(orderCode));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }


    @Override
    public ApiResult<Boolean> scanAliPayCallBack(Map<String, String> alipayRequest) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            companyRechargeManager.scanAliPayCallBack(alipayRequest);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
